package com.ruoyi.bantong.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.bantong.mapper.BtUserMapper;
import com.ruoyi.bantong.service.IBtUserAccountRecordService;
import com.ruoyi.common.core.domain.entity.BtUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bantong.mapper.BtUserTransferMapper;
import com.ruoyi.bantong.domain.BtUserTransfer;
import com.ruoyi.bantong.service.IBtUserTransferService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户转让记录Service业务层处理
 * 
 * @author laohei
 * @date 2021-02-04
 */
@Service
public class BtUserTransferServiceImpl implements IBtUserTransferService 
{
    @Autowired
    private BtUserTransferMapper btUserTransferMapper;

    @Autowired
    private BtUserMapper userMapper;

    @Autowired
    private IBtUserAccountRecordService accountRecordService;

    /**
     * 查询用户转让记录
     * 
     * @param utId 用户转让记录ID
     * @return 用户转让记录
     */
    @Override
    public BtUserTransfer selectBtUserTransferById(Integer utId)
    {
        return btUserTransferMapper.selectBtUserTransferById(utId);
    }

    /**
     * 查询用户转让记录列表
     * 
     * @param btUserTransfer 用户转让记录
     * @return 用户转让记录
     */
    @Override
    public List<BtUserTransfer> selectBtUserTransferList(BtUserTransfer btUserTransfer)
    {
        return btUserTransferMapper.selectBtUserTransferList(btUserTransfer);
    }

    /**
     * 新增用户转让记录
     * 
     * @param btUserTransfer 用户转让记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBtUserTransfer(BtUserTransfer btUserTransfer)
    {
        //判断余额
        BtUser user = userMapper.selectBtUserById(btUserTransfer.getUserId());
        BigDecimal btBean = user.getBtBean(); //金豆余额
        if(btUserTransfer.getTransferBean().compareTo(btBean) == 1){
            throw new CustomException("金豆余额不足！");
        }
        String otherCard = btUserTransfer.getOtherCard(); //20210317 改成使用手机号
        BtUser btUser = userMapper.selectBtUserByPhoneAndStatus(otherCard);
        if(StringUtils.isNull(btUser)){
            throw new CustomException("转让账户错误或该账户未认证！");
        }
        int i = 0;
        try {
            i = btUserTransferMapper.insertBtUserTransfer(btUserTransfer);
            //转让支出
            accountRecordService.updataUserAccount(btUserTransfer.getUserId(), btUserTransfer.getTransferBean(),BigDecimal.ZERO,btUserTransfer.getOsid(),
                                                            "bt_user_transfer","out",9);
            //转让收入
            accountRecordService.updataUserAccount(btUser.getUserId(), btUserTransfer.getTransferBean(),BigDecimal.ZERO,btUserTransfer.getOsid(),
                    "bt_user_transfer","in",10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        btUserTransfer.setCreateTime(DateUtils.getNowDate());
        return i;
    }

    /**
     * 修改用户转让记录
     * 
     * @param btUserTransfer 用户转让记录
     * @return 结果
     */
    @Override
    public int updateBtUserTransfer(BtUserTransfer btUserTransfer)
    {
        btUserTransfer.setUpdateTime(DateUtils.getNowDate());
        return btUserTransferMapper.updateBtUserTransfer(btUserTransfer);
    }

    /**
     * 批量删除用户转让记录
     * 
     * @param utIds 需要删除的用户转让记录ID
     * @return 结果
     */
    @Override
    public int deleteBtUserTransferByIds(Integer[] utIds)
    {
        return btUserTransferMapper.deleteBtUserTransferByIds(utIds);
    }

    /**
     * 删除用户转让记录信息
     * 
     * @param utId 用户转让记录ID
     * @return 结果
     */
    @Override
    public int deleteBtUserTransferById(Integer utId)
    {
        return btUserTransferMapper.deleteBtUserTransferById(utId);
    }
}
